package com.example.springbootvuetest.Utils;

import com.example.springbootvuetest.service.IOrderService;

import java.util.concurrent.DelayQueue;

/**
 * @Description: 订单任务处理器
 * @Author shuoye
 * @Date 2023/3/19
 * @Version 1.0
 */
public class OrderTaskHandler {
    public static IOrderService orderService = null;

    //延迟消息队列
    private static DelayQueue<OrderTask> delayQueue = new DelayQueue<>();

    public static void run() {
        new Thread(() -> {
            System.out.println("Success !");
            while (true) {
                if (orderService != null && delayQueue.size() > 0) {
                    OrderTask orderTask = delayQueue.poll();
                    if (orderTask != null) {
                        if (orderService.updateOrder(orderTask.getOrder())) {
                            System.out.println("成功取消订单：" + orderTask.getOrder());
                        } else {
                            System.out.println("取消任务：" + orderTask.getOrder());
                        }

                    }
                }
            }
        }).start();

    }

    public static void addOrder(OrderTask o) {
        System.out.println("添加任务：" + o);
        delayQueue.put(o);
    }
}
